package edu.whut.mall.admin.shiro.util;

import edu.whut.mall.admin.dto.ShiroSysUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import java.util.List;

/**
 * Author: smq
 * Unit: D9lab
 * Date: 2021-06-11 17.40
 */

@Slf4j
public class ShiroUtil {

    public static Session getSession() {
        return SecurityUtils.getSubject().getSession();
    }

    public static Subject getSubject() {
        return SecurityUtils.getSubject();
    }

    public static ShiroSysUser getUserEntity() {
        return (ShiroSysUser) SecurityUtils.getSubject().getPrincipal();
    }

    public static Integer getUserId() {
        return getUserEntity().getAdminId();
    }

    public static List<Integer> getMerchantId() {
        return getUserEntity().getMerchantId();
    }

    public static List<Integer> getAdminType() {
        return getUserEntity().getAdminType();
    }

    public static void setSessionAttribute(Object key, Object value) {
        getSession().setAttribute(key, value);
    }

    public static Object getSessionAttribute(Object key) {
        return getSession().getAttribute(key);
    }

    public static boolean isLogin() {
        return SecurityUtils.getSubject().getPrincipal() != null;
    }

    public static void logout() {
        SecurityUtils.getSubject().logout();
    }
}
